/*
 * File     : AbstractLargeParameter.java
 * Created  : 1 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;


/**
 * Special parameter implementation for passing parameters through a buffer.
 * Used when the number of parameters exceeds a limit.
 *
 * @author Matthew Wilson
 */
abstract class AbstractLargeParameter extends Parameter
{

    private final List< Parameter > parameters = new ArrayList< Parameter >();


    /**
     * @param parameters
     *            the parameters to send
     */
    AbstractLargeParameter( final List< Parameter > parameters )
    {
        this.parameters.addAll( parameters );
        setNativeTypeAsPointer();
    }

    /**
     * @return the number of natively sized words used
     */
    abstract int getWords();

    /**
     * {@inheritDoc}
     */
    @Override
    int getJavaVarSlots()
    {
        int slots = 0;
        for ( Parameter parameter : this.parameters )
        {
            slots += parameter.getJavaVarSlots();
        }
        return slots;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void allocateFields( final ClassWriter classWriter, final Map< String, Parameter > createdFields )
    {
        for ( Parameter parameter : this.parameters )
        {
            parameter.allocateFields( classWriter, createdFields );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeStaticInitialiserFragment( final MethodVisitor methodVisitor )
    {
        for ( Parameter parameter : this.parameters )
        {
            parameter.writeStaticInitialiserFragment( methodVisitor );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    int allocatePrepareVarSlots( final MethodVisitor methodVisitor, final int varCount )
    {
        int slots = 1;
        for ( Parameter parameter : this.parameters )
        {
            slots += parameter.allocatePrepareVarSlots( methodVisitor, varCount + slots );
        }

        return slots;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeScratchMemorySize( final MethodVisitor methodVisitor )
    {
        // TODO v1.1 large parameter support
        throw new UnsupportedOperationException( "FIXME: Implement large parameter support" );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    boolean needsScratchMemory()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeGetParameter( final MethodVisitor methodVisitor )
    {
        writeGetScratchPointer();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeTidyParameter( final MethodVisitor methodVisitor,
                             final boolean isExceptionHandlerBlock )
    {
        for ( ListIterator< Parameter > it = this.parameters.listIterator( this.parameters.size() );
              it.hasPrevious(); )
        {
            it.previous().writeTidyParameter( methodVisitor, isExceptionHandlerBlock );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    boolean needsTidying()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void finishedMethod( final MethodVisitor methodVisitor,
                         final Label methodStart )
    {
        for ( Parameter parameter : this.parameters )
        {
            parameter.finishedMethod( methodVisitor, methodStart );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void writeReturnParameter( final MethodVisitor methodVisitor, final int returnVarIndex )
    {
        throw new UnsupportedOperationException();
    }


    /**
     * @return the parameters
     */
    final List< Parameter > getParameters()
    {
        return this.parameters;
    }

}
